<?php
declare (strict_types = 1);

namespace app\admin\controller;


use app\BaseController;
use app\middleware\admin\Auth;
use app\model\Coupon as CouponModel;
use think\facade\Db;

class Coupon extends BaseController
{
    protected $middleware=[Auth::class];
    public function index()
    {
        $data=CouponModel::order('id','desc')->paginate(10);
        foreach ($data as &$item){
            $item->end_time=date('Y-m-d H:i',$item->end_time);
            $item->start_time=date('Y-m-d H:i',$item->start_time);
        }
        return $this->success($data);
    }


    public function create()
    {

        CouponModel::create(request()->param());

        Db::table('admin_log')->insert([
            'type'=>3,
            'admin_id'=>request()->userid,
            'content'=>'發送優惠券',
            'created_at'=>time()
        ]);
        return $this->success();
    }


    public function update($id)
    {
        return $this->success(CouponModel::where('id',$id)->update(request()->param()));
    }


    public function delete($id)
    {
        CouponModel::where('id',$id)->delete();
        return $this->success();
    }
}
